package com.lw.lost.utils

import android.content.Context
import android.content.SharedPreferences

object PreferenceUtils {
    private const val PREF_NAME = "LostPrefs"
    private const val KEY_USER_ID = "user_id"
    private const val KEY_USER_NAME = "user_name"
    private const val KEY_IS_LOGGED_IN = "is_logged_in"
    private const val KEY_IS_ADMIN = "is_admin"

    private fun getPreferences(context: Context): SharedPreferences {
        return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
    }

    fun saveUserInfo(context: Context, userId: Long, name: String, isAdmin: Boolean = false) {
        getPreferences(context).edit().apply {
            putLong(KEY_USER_ID, userId)
            putString(KEY_USER_NAME, name)
            putBoolean(KEY_IS_LOGGED_IN, true)
            putBoolean(KEY_IS_ADMIN, isAdmin)
            apply()
        }
    }

    fun getUserId(context: Context): Long {
        return getPreferences(context).getLong(KEY_USER_ID, -1)
    }

    fun getUserName(context: Context): String {
        return getPreferences(context).getString(KEY_USER_NAME, "") ?: ""
    }

    fun isLoggedIn(context: Context): Boolean {
        return getPreferences(context).getBoolean(KEY_IS_LOGGED_IN, false)
    }

    fun isAdmin(context: Context): Boolean {
        return getPreferences(context).getBoolean(KEY_IS_ADMIN, false)
    }

    fun clearUserInfo(context: Context) {
        getPreferences(context).edit().clear().apply()
    }
} 